using MediatR;
using Warehouse.API.Read.App.Commands;
using Warehouse.Dto;
using Warehouse.Repository;

namespace Warehouse.API.Read.App.Handlers
{
    /// <summary>
    /// 调拨单详情查询处理器
    /// </summary>
    public class AllocationDetailQueryHandler : IRequestHandler<AllocationDetailQuery, AllocationDetailQueryDto?>
    {
        private readonly IAllocationRepository _allocationRepository;

        public AllocationDetailQueryHandler(IAllocationRepository allocationRepository)
        {
            _allocationRepository = allocationRepository;
        }

        public async Task<AllocationDetailQueryDto?> Handle(AllocationDetailQuery request, CancellationToken cancellationToken)
        {
            // 获取调拨单基本信息
            var transferOrder = await _allocationRepository.GetDetailWithLinesAsync(request.TransferId);
            if (transferOrder == null)
            {
                return null;
            }

            // 获取调拨单明细
            var lines = await _allocationRepository.GetLinesAsync(request.TransferId);

            // 构建详情DTO
            var detail = new AllocationDetailQueryDto
            {
                TransferId = transferOrder.Id,
                TransferNo = transferOrder.TransferNo,
                ApplyDate = transferOrder.ApplyDate,
                TransferType = transferOrder.TransferType,
                FromWarehouseId = transferOrder.FromWarehouseId,
                FromWarehouseName = GetWarehouseName(transferOrder.FromWarehouseId),
                ToWarehouseId = transferOrder.ToWarehouseId,
                ToWarehouseName = GetWarehouseName(transferOrder.ToWarehouseId),
                BillStatus = transferOrder.BillStatus,
                BillStatusName = GetBillStatusName(transferOrder.BillStatus),
                OutStatus = transferOrder.OutStatus,
                OutStatusName = GetOutStatusName(transferOrder.OutStatus),
                InStatus = transferOrder.InStatus,
                InStatusName = GetInStatusName(transferOrder.InStatus),
                TotalQty = transferOrder.TotalQty,
                TotalAmount = transferOrder.TotalAmount,
                HandlerName = transferOrder.HandlerName ?? "",
                ApplicantName = transferOrder.ApplicantName ?? "",
                Remark = transferOrder.Remark ?? "",
                CreatedAt = transferOrder.CreatedAt,
                UpdatedAt = transferOrder.UpdatedAt,
                Lines = lines.Select(line => new AllocationLineQueryDto
                {
                    Id = line.Id,
                    TransferId = line.TransferId,
                    LineNo = line.LineNo,
                    ProductId = line.ProductId,
                    ProductName = line.ProductName,
                    ProductNo = line.ProductNo,
                    Specification = line.Specification,
                    Unit = line.Unit,
                    FromLocationId = line.FromLocationId,
                    ToLocationId = line.ToLocationId,
                    FromLocationName = GetLocationName(line.FromLocationId),
                    ToLocationName = GetLocationName(line.ToLocationId),
                    ReqQty = line.ReqQty,
                    OutQty = line.OutQty,
                    InQty = line.InQty,
                    UnitPrice = line.UnitPrice
                }).ToList()
            };

            return detail;
        }

        private static string GetWarehouseName(long warehouseId)
        {
            // 这里应该从数据库查询仓库名称，暂时返回模拟数据
            return warehouseId switch
            {
                1 => "成品仓库一",
                2 => "成品仓库二",
                3 => "原料仓库一",
                4 => "原料仓库二",
                _ => $"仓库{warehouseId}"
            };
        }

        private static string GetLocationName(long? locationId)
        {
            if (!locationId.HasValue) return "";
            
            // 这里应该从数据库查询库位名称，暂时返回模拟数据
            return locationId.Value switch
            {
                1 => "A区-01",
                2 => "A区-02",
                3 => "B区-01",
                4 => "B区-02",
                5 => "C区-01",
                6 => "C区-02",
                7 => "D区-01",
                8 => "D区-02",
                _ => $"库位{locationId.Value}"
            };
        }

        private static string GetBillStatusName(byte billStatus)
        {
            return billStatus switch
            {
                10 => "待审核",
                20 => "待审核",
                30 => "待出库",
                60 => "待入库",
                100 => "已完成",
                90 => "驳回",
                _ => "未知状态"
            };
        }

        private static string GetOutStatusName(byte outStatus)
        {
            return outStatus switch
            {
                0 => "未出库",
                1 => "部分出库",
                2 => "已出库",
                _ => "未知状态"
            };
        }

        private static string GetInStatusName(byte inStatus)
        {
            return inStatus switch
            {
                0 => "未入库",
                1 => "部分入库",
                2 => "已入库",
                _ => "未知状态"
            };
        }
    }
}

